//
//  ScoreViewController.m
//  BaseProject
//
//  Created by hundredsandc on 2017/10/25.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "ScoreViewController.h"
#import "HTTPManagers.h"
#import "ScoreTableViewCell.h"
#import "ScoreDetaillViewController.h"
#import "ScoreModel.h"
#import "MJExtension.h"

@interface ScoreViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UISegmentedControl *segment;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArr;
@end

@implementation ScoreViewController
{
    NSInteger page;
    NSInteger type;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    type = 0;
    [self getData];
    self.navigationItem.titleView = self.segment;
    [self.view addSubview:self.tableView];
}

-(void)getData{
//    http://www.buyinball.com/app-web/api/match/qry_matchs?league_id=&status=1&match_diff_day=0
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"leagueid"]=@"";
    params[@"status"] = @(1);
    params[@"match_diff_day"] = @(type);
    
    
    [HTTPManagers getWithScoreURL:@"qry_matchs" params:params success:^(id json) {
        [_tableView.mj_header endRefreshing];
        if ([[json objectForKey:@"result_msg"]isEqualToString:@"成功"]) {
            NSDictionary *qry_matchs = json[@"qry_matchs"];
            NSArray *arr = qry_matchs[@"data"];
            if (self.dataArr.count>0) {
                [self.dataArr removeAllObjects];
            }
            for (int i =0; i<arr.count; i++) {
                NSDictionary *dict = arr[i];
                ScoreModel *model = [ScoreModel mj_objectWithKeyValues:dict];
                [self.dataArr addObject:model];

            }
            [_tableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)changeSegment:(UISegmentedControl *)Seg{
    NSInteger Index = Seg.selectedSegmentIndex;
    [_tableView setContentOffset:CGPointMake(0, 0) animated:NO];
    if (Index==0) {
        type=0;
    }else if (Index==1){
        type = 1;//赛程
    }else{
        type=-1;//赛果
    }
    [self getData];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.dataArr.count>0) {
        ScoreModel *model = self.dataArr[indexPath.row];
    ScoreDetaillViewController *detail = [ScoreDetaillViewController new];
    
        detail.match_id = model.match_id;
        detail.scoreModel = model;
        
    [self.navigationController pushViewController:detail animated:YES];
    }

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ScoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ScoreTableViewCell class]) forIndexPath:indexPath];
    if (self.dataArr.count>0) {
        
        ScoreModel *model = self.dataArr[indexPath.row];
        [cell customTitle:model.league_name time:model.group_date teamOneRed:model.home_red_card teamOneYellow:model.h_yellow teamOneName:model.home_team_name teamOneScore:model.home_team_score teamTwoRed:model.guest_red_card teamTwoYellow:model.g_yellow teamTwoName:model.guest_team_name teamTwoScore:model.guest_team_score leftSub:model.asian_odds rightSub:model.europe_odds];
    }
    
    return cell;
    
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-FTab) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ScoreTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ScoreTableViewCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getData];
            
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            page++;
            if (_segment.selectedSegmentIndex==0) {
                
            }else if(_segment.selectedSegmentIndex == 1){
                
            }else{
            
            }
            [_tableView.mj_footer endRefreshing];
        }];
    }
    return _tableView;
}
-(UISegmentedControl *)segment{
    if (!_segment) {
        _segment = [[UISegmentedControl alloc]initWithItems:@[@"即时",@"赛程",@"赛果"]];
        UIFont *font = [UIFont boldSystemFontOfSize:16.0f];
        [_segment setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:font} forState:UIControlStateSelected];
        [_segment setTitleTextAttributes:@{NSFontAttributeName:font} forState:UIControlStateNormal];
        _segment.size = CGSizeMake(231, 28);
        _segment.selectedSegmentIndex = 0;
        _segment.tintColor = RGBCOLOR(0x25, 0x43, 0x7f);
        [_segment addTarget:self action:@selector(changeSegment:) forControlEvents:UIControlEventValueChanged];
    }
    return _segment;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
@end
